using UnityEngine.Events;

namespace JEngine.Framework
{
    public struct TChange<T>
    {
        public class OnValueChaneg : UnityEvent<T>
        {
        }

        private T now;
        private T old;

        private OnValueChaneg chaneg;

        public OnValueChaneg onValueChange
        {
            get
            {
                if (chaneg == null)
                {
                    chaneg = new OnValueChaneg();
                }

                return chaneg;
            }
        }

        public T value
        {
            get { return now; }
            set
            {
                now = value;
                if (old == null)
                {
                    onValueChange?.Invoke(now);
                }
                else
                {
                    if (!old.Equals(now))
                    {
                        onValueChange?.Invoke(now);
                    }
                }

                old = now;
            }
        }
    }

    public struct TRefresh<T>
    {
        public class OnValueRefresh : UnityEvent<T>
        {
        }

        private T now;

        private OnValueRefresh refresh;

        public OnValueRefresh onValueRefresh
        {
            get
            {
                if (refresh == null)
                {
                    refresh = new OnValueRefresh();
                }

                return refresh;
            }
        }

        public T value
        {
            get { return now; }
            set
            {
                now = value;
                onValueRefresh?.Invoke(now);
            }
        }
    }
}